//
//  CitiesViewController_iPad.m
//  RestaurantX
//
//  Created by Vinh Huynh on 6/25/12.
//  Copyright (c) 2012 _eBars GbR_. All rights reserved.
//

#import "CitiesViewController_iPad.h"
#import "CityListParser.h"
#import "DSActivityView.h"
#import "HomeViewController_iPad.h"
#import "RegBarsAppDelegate_iPad.h"


@interface CitiesViewController_iPad ()<UITableViewDataSource, UITableViewDelegate, CityListParserDelegate>
{
    NSMutableArray *cityList;
}

@end

@implementation CitiesViewController_iPad
@synthesize cityTable;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    cityList = [[NSMutableArray alloc] init];
    self.title = @"Städte";
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [self setCityTable:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
	return YES;
}

- (void)updateListCities:(NSString*)stateId{
    [DSBezelActivityView newActivityViewForView:self.view withLabel:@"loading..."];
    CityListParser *cityListParser = [[[CityListParser alloc] init] autorelease];
    [cityListParser getCitiesInState:stateId withDelegate:self];
    cityList = [[NSMutableArray alloc] init];
}

#pragma mark -
#pragma mark CityListParser Delegate

- (void)didEndGetCities:(NSArray*)resultDict success:(BOOL) yn{
    if (resultDict) {
        [cityList setArray:resultDict];
    }
    NSLog(@"didEndGetCityList : %@", cityList);
    [cityTable reloadData];
    [DSBezelActivityView removeViewAnimated:YES];
}

#pragma mark - UITableview Delegate & Datasource 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [cityList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }
    NSDictionary *dict = [cityList objectAtIndex:indexPath.row];
    cell.textLabel.text = [dict objectForKey:@"name"];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dict = [cityList objectAtIndex:indexPath.row];
    mainAppDelegate_iPad.currentCityId = [dict objectForKey:@"id"];
    HomeViewController_iPad *homeViewController = [[HomeViewController_iPad alloc] initWithNibName:@"HomeViewController_iPad" bundle:nil];
    [self.navigationController pushViewController:homeViewController animated:YES];
    [homeViewController loadRestaurant];
}

- (void)flipToHome{
    self.cityTable.hidden = NO;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0f];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
                           forView:[self view]
                             cache:YES]; 
    [UIView commitAnimations];
}

- (void)dealloc {
    [super dealloc];
}
@end
